package com.haojiu.controller;

import com.github.pagehelper.PageInfo;
import com.haojiu.pojo.LendList;
import com.haojiu.pojo.Notice;
import com.haojiu.pojo.User;
import com.haojiu.service.NoticeService;
import com.haojiu.service.UserService;
import com.haojiu.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/27 21:11
 * create in RuGao JiangSu
 */
@Controller
public class NoticeController {
    @Autowired
    @Qualifier("NoticeServiceImpl")
    private NoticeService noticeService;

    @RequestMapping("/noticeIndexOfBack")
    public ModelAndView queryAllNotice(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        List<Notice> list = noticeService.queryAllNotice(page, size);
        List<Notice> noticeList = noticeService.queryAllNotice();
        mv.addObject("noticeList",noticeList);
        PageInfo<Notice> pageInfo = new PageInfo<>(list);
        mv.addObject("pageInfo", pageInfo);
        HttpSession session = request.getSession();
        String userType = (String) session.getAttribute("userType");
        if(userType.equals("manager")){
            mv.setViewName("notice/allNotice");
        }else {
            mv.setViewName("notice/allNotice1");
        }
        return mv;
    }

    @RequestMapping("/login/delNotice/{noticeTheme}")
    public String deleteNotice(@PathVariable("noticeTheme") String noticeTheme) {
       noticeService.deleteNoticeByTheme(noticeTheme);
        return "redirect:/noticeIndexOfBack";
    }

    @RequestMapping("/login/toAddNotice")
    public String toAddNotice() {
        return "notice/addNotice";
    }

    @RequestMapping("/login/addNotice")
    public String addNotice(@RequestParam("noticeTheme") String noticeTheme, @RequestParam("noticeContent") String noticeContent, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(username);
        Date timePu = new Date();
        Notice notice = new Notice(noticeTheme, noticeContent, username, timePu);
        System.out.println(notice);
        noticeService.addNotice(notice);
        return "redirect:/noticeIndexOfBack";
    }

    @RequestMapping("/login/searchNotice")
    public String getUser(@RequestParam("keyNoticeTheme") String noticeTheme, Model model) {
        Notice notice = noticeService.queryNoticeByTheme(noticeTheme);
        if (notice != null) {
            List<Notice> noticeList = new ArrayList();
            noticeList.add(notice);
            model.addAttribute("one", noticeList);
            return "notice/oneNotice";
        } else {
            return "redirect:/noticeIndexOfBack";
        }
    }
    @RequestMapping("/login/exportNotice")
    public void export(HttpServletRequest request, HttpServletResponse response){
        List<Notice> noticeList = noticeService.queryAllNotice();
        ExportExcel<Notice> ee= new ExportExcel<Notice>();
        String[] headers = {"公告主题","公告内容","发布人","发布时间"};
        String fileName = "阅读之家公告excel表";
        ee.exportExcel(headers,noticeList,fileName,response);
    }
}
